package com.xb.loan.util.file;


import com.xb.loan.util.log.DefaultLoggerFacotry;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;



/**
 * 
 * @author heipn
 * @version 1.01 Mar 31, 2009 palmutil
 * 
 */
public class FileHelper {
	private static Logger logger = DefaultLoggerFacotry.getLogger(FileHelper.class);
    
	/**
	 * @param path
	 * @param msg
	 * @param noExistCreate
	 */
	public static void wirteNoAppend(String path, String msg,
			boolean noExistCreate){
		FileWriter out = null;
		try {
			File file = new File(path);
			if (noExistCreate && !file.exists()) {
				File parent = file.getParentFile();
				if(!parent.exists()){
					parent.mkdirs();
				}
				file.createNewFile();
			}
			if(file.exists()){
				out = new FileWriter(file);
				out.write(msg);
				out.flush();
			}
		} catch (Exception e) {
		    logger.error("",e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {
				}
			}
		}
	}
	
	/**
	 * @param path
	 * @return
	 */
	public static List<String> readFileByLine(String path){
		  BufferedReader in = null;
		  try
		  {
		    File file = new File(path);
		    if(file.exists() && file.isFile()){
		    	String filePath = path;
		    	in = new BufferedReader(new FileReader(filePath));
		    	List<String> list = new ArrayList<String>();
		    	String temp = null;
		    	while((temp = in.readLine())!= null){
		    	   list.add(temp);
		    	}
		    	return list;
		    }
		  }catch(Exception e){
			  e.printStackTrace();
		  }finally{
			  if(in != null){
				try{in.close();}catch(Exception e){}
			  }
		  }
		    return null;
	}
	
	/**
	 * @param floderPath
	 * @param nameRexp
	 * @return
	 */
	public static List<String> findFileNames(String floderPath,String nameRexp){
		 List<String> fileNames = new ArrayList<String>();
		 File file = new File(floderPath);
		 if(file.exists() && file.isDirectory()){
			 File[] files = file.listFiles();
			 if(files != null && files.length > 0){
			    
			     for(File temp:files){
			    	 String name = temp.getName();
			    	 if(name.matches(nameRexp)){
			    		 fileNames.add(name);
			    	 }
			     }
			 }
		 }
		 return fileNames;
	}
	
	
	/**
	 * @param floderPath
	 * @param nameRexp
	 * @return
	 */
	public static List<File> findFiles(String floderPath,String nameRexp){
		 List<File> matchFiles = null;
		 File file = new File(floderPath);
		 if(file.exists() && file.isDirectory()){
			 File[] files = file.listFiles();
			 if(files != null && files.length > 0){
			     matchFiles = new ArrayList<File>();
			     for(File temp:files){
			    	 String name = temp.getName();
			    	 if(name.matches(nameRexp)){
			    		 matchFiles.add(temp);
			    	 }
			     }
			 }
		 }
		 return matchFiles;
	}
	/**
	 * @param filePath
	 * @param newName
	 */
	public synchronized static boolean renameFile(String filePath,String newFilePath){
		boolean result = false;
		File file = new File(filePath);
		if(file.exists()&& file.isFile()){
			result = file.renameTo(new File(newFilePath));
		}
		return result;
	}
	
	/**
	 * 追加文件内容
	 * @param file
	 * @param msgs
	 */
	public static void writeFileAppand(File file,List<String> msgs){
		FileWriter out = null;
		try {
			    String lineSeparator = System.getProperty("line.separator");
				out = new FileWriter(file,true);
				for(String s:msgs){
					out.write(s);
					out.write(lineSeparator);
				}
				out.flush();
		
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("",e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 根据文件判断本地文件是否存在
	 * 
	 * @author zheng.p
	 * @param file 本地文件
	 * @return 文件是否存在
	 * @date 2015年9月26日 上午11:13:17
	 */
	public static boolean isFileExists(File file){
	    return file.exists();
	}
	
	/**
	 * 根据文件名判断本地文件是否存在
	 * 
	 * @author zheng.p
	 * @param localFile 本地路径名称
	 * @return 文件是否存在
	 * @date 2015年9月26日 上午11:13:42
	 */
	public static boolean isFileExists(String localFile){
	    return isFileExists(new File(localFile));
	}
	
	public static void main(String[] args){
//		System.out.println(renameFile("c:\\ceshi.txt.bak","c:\\TradeInterface2.xml"));
//		System.out.println("2009802.prizeThread-ing".matches("(\\d+\\.\\w+)-ing"));
//		System.out.println("shahuaitao"+System.getProperty("line.separator")+"sa");
		System.out.println("17005671");
		String data="17005671";
		int length=data.length();
		StringBuffer sb=new StringBuffer();
		sb.append("e://download//").append(data.substring(length-2)).append(File.separatorChar)
		.append(data.substring(length-4,length-2)).append(File.separator).append(data.substring(length-6,length-4))
		.append(File.separator).append("userinfo.xml");
		System.out.println(sb.toString());
		FileHelper.wirteNoAppend(sb.toString(),"shahuaitao", true);
	}
}
